<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        input{
            width: 15px;
            height: 15px;
            border: 1px solid palegreen;
        }
    </style>
</head>
<body>
    <input type="checkbox">
    <p>
        <input type="checkbox">
        <input type="checkbox">
        <input type="checkbox">
        <input type="checkbox">
    </p>
    <script>
        var all = document.querySelector('input')
        var odd = document.querySelectorAll('p input')
        var n = 0
        all.onchange = function(){
            console.log(this.checked)
            for(var i=0;i<odd.length;i++){
                // if(this.checked){
                //     odd[i].checked = true
                //     n = odd.length
                // }else{
                //     odd[i].checked = false
                //     n = 0
                // }
                odd[i].checked = this.checked
                // this.checked ? n =odd.length : n=0
                n = this.checked ? odd.length : 0
            }
        }

        for(var key in odd){
            odd[key].onchange = function(){       
                this.checked ? n++ : n--
                n==odd.length ? all.checked=true : all.checked = false
                // all.checked = n === odd.length  
            }    
        }

    </script>
</body>
</html>